package com.InvoicingSystem.service.impl;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Service;

import com.InvoicingSystem.dao.SaleMapper;
import com.InvoicingSystem.pojo.Sale;
import com.InvoicingSystem.pojo.UserInfo;
import com.InvoicingSystem.service.SaleServices;
import com.InvoicingSystem.util.MyBatisUtil;
@Service
public class SaleServicesImpl implements SaleServices {

	@Override
	public boolean addSale(Sale sale, UserInfo userInfo) {
		sale.setUserid(userInfo.getId());
		sale.setTotalprice(sale.getPrice() * sale.getQuantity());

		SqlSession session = MyBatisUtil.getSession();
		SaleMapper salsMapper = session.getMapper(SaleMapper.class);
		int resultset = salsMapper.addSale(sale);
		if (resultset > 0) {
			return true;
		}
		return false;
	}

	@Override
	public List<Map<String, Object>> findPageAll(Integer indexPage, Integer pageSize, Integer orderByIndex) {
		SqlSession session = MyBatisUtil.getSession();
		SaleMapper saleMapper = session.getMapper(SaleMapper.class);

		indexPage = (indexPage - 1) * pageSize;
		List<Map<String, Object>> list = saleMapper.findPageAll(indexPage, pageSize, orderByIndex);

		return list;
	}
	public int findTotalPage(Integer pageSize) {
		SqlSession session = MyBatisUtil.getSession();
		SaleMapper saleMapper = session.getMapper(SaleMapper.class);

		int resultset = saleMapper.getTotalPage();


		return resultset % pageSize == 0 ? resultset / pageSize : resultset / pageSize + 1;
	}

	@Override
	public int findTotalTable() {
		SqlSession session = MyBatisUtil.getSession();
		SaleMapper saleMapper = session.getMapper(SaleMapper.class);

		int resultset = saleMapper.getTotalPage();


		return resultset;
	}

}
